package owl2vowl.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import owl2vowl.Owl2Vowl;

@RestController
public class Owl2VowlController {
	private static final Logger logger = LogManager.getLogger(Owl2VowlController.class);
	private static final Logger conversionLogger = LogManager.getLogger("conversion-logger");
	private static final String 小ONVERT_MAPPING = "/convert";
	private static final String READ_JSON = "/read";

	@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Parameter not correct")
	@ExceptionHandler({IllegalArgumentException.class})
	public void parameterExceptionHandler(Exception e) {
		logger.info("--- Parameter exception: " + e.getMessage());
		conversionLogger.error("Problems with parameters: " + e.getMessage());
	}

	@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Ontology could not be created")
	@ExceptionHandler({OWLOntologyCreationException.class})
	public void ontologyCreationExceptionHandler(Exception e) {
		logger.info("--- Ontology creation exception: " + e.getMessage());
		conversionLogger.error("Problems in ontology creation process: " + e.getMessage());
	}

	@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Problems while generating uploaded file on server.")
	@ExceptionHandler({IOException.class})
	public void fileExceptionHandler(Exception e) {
		logger.info("--- IO exception: " + e.getMessage());
		conversionLogger.error("IO exception while generating file on server: " + e.getMessage());
	}

	@RequestMapping(value = {"/convert"}, method = {RequestMethod.POST})
	public String uploadOntology(@RequestParam("ontology") MultipartFile[] files)
			throws IOException, OWLOntologyCreationException {
		if (files != null && files.length != 0) {
			if (files.length > 1) {
				throw new IllegalArgumentException("Please upload only the main ontology!");
			} else {
				ArrayList inputStreams = new ArrayList();
				MultipartFile[] jsonAsString = files;
				int e = files.length;

				for (int arg4 = 0; arg4 < e; ++arg4) {
					MultipartFile file = jsonAsString[arg4];
					inputStreams.add(file.getInputStream());
				}

				try {
					Owl2Vowl arg8 = new Owl2Vowl((InputStream) inputStreams.get(0));
					String arg7 = arg8.getJsonAsString();
					return arg7;
				} catch (Exception arg6) {
					conversionLogger.info("Uploaded files " + files[0].getName() + ": " + 0);
					throw arg6;
				}
			}
		} else {
			throw new IllegalArgumentException("No file uploaded!");
		}
	}

	@RequestMapping(value = {"/convert"}, method = {RequestMethod.GET})
	public String convertIRI(@RequestParam("iri") String iri) throws IOException, OWLOntologyCreationException {
		String out = iri.replace(" ", "%20");

		String jsonAsString;
		try {
			Owl2Vowl e = new Owl2Vowl(IRI.create(out));
			jsonAsString = e.getJsonAsString();
		} catch (Exception arg4) {
			conversionLogger.info(out + " " + 1);
			throw arg4;
		}

		conversionLogger.info(iri + " " + 0);
		return jsonAsString;
	}

	@RequestMapping(value = {"/read"}, method = {RequestMethod.POST})
	public String uploadJSON(@RequestParam("ontology") MultipartFile[] files)
			throws IOException, OWLOntologyCreationException {
		if (files != null && files.length != 0) {
			if (files.length > 1) {
				throw new IllegalArgumentException("Please upload only the main ontology!");
			} else {
				ArrayList inputStreams = new ArrayList();
				String jsonAsString = "";
				MultipartFile[] e = files;
				int arg4 = files.length;

				for (int arg5 = 0; arg5 < arg4; ++arg5) {
					MultipartFile file = e[arg5];
					inputStreams.add(file.getInputStream());
				}

				try {
					jsonAsString = IOUtils.toString((InputStream) inputStreams.get(0));
				} catch (Exception arg7) {
					logger.info("Something went wrong");
					conversionLogger.error("Something went wrong " + arg7.getMessage());
				}

				return jsonAsString;
			}
		} else {
			throw new IllegalArgumentException("No file uploaded!");
		}
	}

	@RequestMapping(value = {"/read"}, method = {RequestMethod.GET})
	public String readJsons(@RequestParam("json") String json) throws IOException, OWLOntologyCreationException {
		String jsonAsString = "";

		try {
			InputStream e = (new URL(json)).openStream();

			try {
				jsonAsString = IOUtils.toString(e);
			} finally {
				IOUtils.closeQuietly(e);
			}
		} catch (Exception arg7) {
			logger.info("Something went wrong");
			conversionLogger.error("Something went wrong " + arg7.getMessage());
		}

		return jsonAsString;
	}
}